package aes.server;

import java.sql.*;
import java.util.ArrayList;

import common.*;

/**
 * This class represents manager used in order to manage users
 *
 */
public class UserManager {
	
	/**
	 * Validate user while login to system
	 * @param login User login name
	 * @param password User password
	 * @return User object (Student, Teacher or Principal)
	 */
	public static User Login(String login, String password)
	{
		User r=null;
		DBManager db = DBManager.getInstance();
		try {
			PreparedStatement stmt = db.getConnection().prepareStatement("SELECT * FROM users WHERE login=? AND password=?;");
			stmt.setString(1, login);
			stmt.setString(2, password);
			ResultSet rs = stmt.executeQuery();
			if(rs.next())
			{
				
				switch(rs.getInt("role")){
				case 1:
					r=new Student();
					break;
				case 2:
					r=new Teacher();
					break;
				case 3:
					r=new Principal();
					break;
				
				}
				
				r.setId(rs.getInt("id"));
				r.setLogin(login);
				r.setFirstname(rs.getString("firstname"));
				r.setLastname(rs.getString("lastname"));
				
			}		
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();	
		}
		return r;
	}
	
	/**
	 * Add new user to the system
	 * @param login Login name
	 * @param firstName First name
	 * @param lastName Last name
	 * @param password Password
	 * @param role Role code
	 * @return User Id or -1 if failed
	 */
	public static int AddUser(String login, String firstName, String lastName, String password, String role){
		DBManager db = DBManager.getInstance();
		try {
			PreparedStatement stmt1 = db.getConnection().prepareStatement("SELECT id FROM users WHERE login=?;");
			stmt1.setString(1, login);
			ResultSet rs1 = stmt1.executeQuery();
			
			if(rs1.next()!=false){
				rs1.close();
				return -1;
			}
			else{
				rs1.close();
				PreparedStatement stmt2 = db.getConnection().prepareStatement("INSERT INTO users (login,firstname,lastname,password,role) VALUES (?,?,?,?,?);");
				stmt2.setString(1, login);
				stmt2.setString(2, firstName);
				stmt2.setString(3, lastName);
				stmt2.setString(4, password);
				stmt2.setInt(5, Integer.parseInt(role));
				//int rs2 = 
			    stmt2.executeUpdate();
				return 1;
			}	
		} catch (SQLException e) {
			e.printStackTrace();
			return -2;
		}
	}

	/**
	 * Retrieve all teachers
	 * @return Array of teachers
	 */
	public static Teacher[] GetTeachers() {
		DBManager db = DBManager.getInstance();
		String sql = "SELECT * FROM aes.users WHERE role=2;";
		try {
			ArrayList<Teacher> ret = new ArrayList<Teacher>();
			PreparedStatement stmt = db.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			
			while (rs.next()) {
				Teacher t = new Teacher();
				t.setId(rs.getInt("id"));
				t.setFirstname(rs.getString("firstname"));
				t.setLastname(rs.getString("lastname"));
				t.setLogin("login");
				
				ret.add(t);
			}
			rs.close();
			Teacher[] res = new Teacher[ret.size()];
			ret.toArray(res);
			return res;
		}
		catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Retrieve all students
	 * @return Array of students
	 */
	public static Student[] GetStudents() {
		DBManager db = DBManager.getInstance();
		String sql = "SELECT * FROM aes.users WHERE role=1;";
		try {
			ArrayList<Student> ret = new ArrayList<Student>();
			PreparedStatement stmt = db.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			
			while (rs.next()) {
				Student t = new Student();
				t.setId(rs.getInt("id"));
				t.setFirstname(rs.getString("firstname"));
				t.setLastname(rs.getString("lastname"));
				t.setLogin("login");
				
				ret.add(t);
			}
			rs.close();
			Student[] res = new Student[ret.size()];
			ret.toArray(res);
			return res;
		}
		catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}
